﻿using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;

namespace LearnWPF.Effects
{
    public class ColorBlindnessEffect : ShaderEffect
    {
        private static readonly PixelShader _pixelShader = new PixelShader();

        static ColorBlindnessEffect()
        {
            _pixelShader.UriSource = Global.MakePackUri("Accessibility/ColorBlindness/ColorBlindness.ps");
        }

        public ColorBlindnessEffect()
        {
            PixelShader = _pixelShader;
            
            UpdateShaderValue(InputProperty);
            UpdateShaderValue(DeficiencyIndexProperty);
        }

        public static readonly DependencyProperty ColorDeficiencyProperty = DependencyProperty.Register("ColorDeficiency", typeof(ColorDeficiency), typeof(ColorBlindnessEffect), new UIPropertyMetadata(ColorDeficiency.Normal, ColorDeficiency_Changed));
        public ColorDeficiency ColorDeficiency
        {
            get { return (ColorDeficiency)GetValue(ColorDeficiencyProperty); }
            set { SetValue(ColorDeficiencyProperty, value); }
        }

        public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(ColorBlindnessEffect), 0);
        public Brush Input
        {
            get { return (Brush)GetValue(InputProperty); }
            set { SetValue(InputProperty, value); }
        }

        public static readonly DependencyProperty DeficiencyIndexProperty = DependencyProperty.Register("DeficiencyIndex", typeof(float), typeof(ColorBlindnessEffect), new UIPropertyMetadata((float)-1, PixelShaderConstantCallback(0)));
        public float DeficiencyIndex
        {
            get { return (float)GetValue(DeficiencyIndexProperty); }
            set { SetValue(DeficiencyIndexProperty, value); }
        }

        private static void ColorDeficiency_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            d.SetValue(DeficiencyIndexProperty, (float)(int)(ColorDeficiency)e.NewValue);
        }
    }
}
